package util;

import java.util.concurrent.TimeUnit;

public class DeadLock {
    public static void main(String[] args) {
        final Object o1 = new Object();
        final Object o2 = new Object();

        new Thread(() -> {
            synchronized (o1) {
                System.out.println("thread 1 get o1 lock");
                try {
                    TimeUnit.MILLISECONDS.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("thread 1 wait o2 lock");
                synchronized (o2) {
                    System.out.println("123");
                }
            }
        }).start();
        new Thread(() -> {
            synchronized (o2) {
                System.out.println("thread 2 get o2 lock");
                try {
                    TimeUnit.MILLISECONDS.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("thread 2 wait o1 lock");
                synchronized (o1) {
                    System.out.println("123");
                }
            }
        }).start();
    }
}
